//
//  Push.swift
//  CTools
//
//  Created by xhl_dabaoMac on 2021/11/17.
//

import UIKit


public class Push {
    
    
    /// 推入一个页面
    /// - Parameter controller: 要推入的controller
    public static func to(_ controller:UIViewController, addNavAuto: Bool = true) {
        var childVc: UIViewController;
        if addNavAuto {
            childVc = CNavController();
            let vc = childVc as! CNavController
            vc.title = controller.title;
            vc.subController = controller;
            
        } else {
            childVc = controller
        }
        let vc = topViewController()
        controller.hidesBottomBarWhenPushed = true
        vc?.navigationController?.pushViewController(childVc, animated: true)
        
    }
    
    
    public static func toView(_ view:UIView,title:String? = nil){
        
        
    }
    
    
    
    
    
    /// 获取顶层的controller
    /// - Parameter controller: controller
    /// - Returns: 递归查找
    public static func topViewController(_ controller:UIViewController? = UIApplication.shared.keyWindow?.rootViewController)-> UIViewController?{
        if let navigationController = controller as? UINavigationController {
            return topViewController(navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(presented)
        }
        return controller
    }
}
